﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows;
using System.IO;

namespace UI.Desktop
{
    public partial class frmReporteTurnos : Form
    {
        public frmReporteTurnos()
        {
            InitializeComponent();
        }

        #region CB PROFESIONAL

        private void cbProfesional_Click(object sender, EventArgs e)
        {
            Business.Entities.Profesional prof = new Business.Entities.Profesional();
            DataSet dsProf = new DataSet();
            dsProf = prof.ObtenerListaProfesional();
            ObtenerProfesional(dsProf);
        }

        private void ObtenerProfesional(DataSet dsProf)
        {
            cbProfesional.Text = "Seleccione el profesional";
            cbProfesional.DataSource = dsProf.Tables["Profesional"];
            string Id_profesional = dsProf.Tables["Profesional"].Columns[0].Caption.ToString();
            string apellido = dsProf.Tables["Profesional"].Columns[0].Caption.ToString();
            cbProfesional.DisplayMember = apellido;
        }

        #endregion

        #region BOTON EMITIR

        private void btnEmitir_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbProfesional.Text == "")
                { 
                    MessageBox.Show("Debe seleccionar un profesional primero", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
                    else
                    {
                        string ruta = Path.Combine(Application.StartupPath, "ReporteTurnos.rpt");
                        ReportDocument rpt = new ReportDocument();
                        //rpt.Load(ruta);
                        rpt.Load(@"C:\SistemaTurnet\Code\turnet_v1.6\UI.Desktop\ReporteTurnos.rpt");
                        Business.Entities.Profesional prof = new Business.Entities.Profesional();
                        Business.Entities.Turno Entur = new Business.Entities.Turno();
                        string Concatenado = cbProfesional.Text;
                        DataSet dsProf = new DataSet();
                        DataSet dsTurnos = new DataSet();
                        dsProf = prof.ObtenerProfesional(Concatenado);
                        string id_profesional = dsProf.Tables["Profesional"].Rows[0][0].ToString();
                        string desde = dtDesde.Value.ToString();
                        string hasta = dtHasta.Value.ToString();

                        ///Aca comparo el Combobox para filtrar por Profesional
                        ParameterFieldDefinitions crParameterFieldDefinitions;
                        ParameterFieldDefinition crParameterFieldDefinition;
                        ParameterValues crParameterValues = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                        crParameterDiscreteValue.Value = id_profesional;
                        crParameterFieldDefinitions = rpt.DataDefinition.ParameterFields;
                        crParameterFieldDefinition = crParameterFieldDefinitions["Profesional"];
                        crParameterValues = crParameterFieldDefinition.CurrentValues;

                        crParameterValues.Clear();
                        crParameterValues.Add(crParameterDiscreteValue);
                        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                        /// Aca comparo con el primer DataPicker la fecha desde
                        ParameterFieldDefinitions crParameterFieldDefinitions1;
                        ParameterFieldDefinition crParameterFieldDefinition1;
                        ParameterValues crParameterValues1 = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                        crParameterDiscreteValue1.Value = desde;
                        crParameterFieldDefinitions1 = rpt.DataDefinition.ParameterFields;
                        crParameterFieldDefinition1 = crParameterFieldDefinitions1["Desde"];

                        crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                        crParameterValues1.Clear();
                        crParameterValues1.Add(crParameterDiscreteValue1);
                        crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);

                        ///Aca comparo el segundo datapicker con la fecha hasta
                        ParameterFieldDefinitions crParameterFieldDefinitions2;
                        ParameterFieldDefinition crParameterFieldDefinition2;
                        ParameterValues crParameterValues2 = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue2 = new ParameterDiscreteValue();

                        crParameterDiscreteValue2.Value = hasta;
                        crParameterFieldDefinitions2 = rpt.DataDefinition.ParameterFields;
                        crParameterFieldDefinition2 = crParameterFieldDefinitions2["Hasta"];

                        crParameterValues2 = crParameterFieldDefinition2.CurrentValues;

                        crParameterValues2.Clear();
                        crParameterValues2.Add(crParameterDiscreteValue2);
                        crParameterFieldDefinition2.ApplyCurrentValues(crParameterValues2);

                        rpt.SetDatabaseLogon("sa", "123");
                        crystalReportViewer1.ReportSource = rpt;
                        crystalReportViewer1.Refresh();                              
                    }
                }
                catch 
                { 
                    MessageBox.Show("Hubo un problema, por favor salga del reporte e intentelo otra vez", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
                }
         }
        #endregion

        private void frmReporteTurnos_Load(object sender, EventArgs e)
        {

        }
    }
}








        

        
        


    


          

    

